//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tuliu.appupdate.updatelib;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.util.Log;

import com.tuliu.appupdate.updatelib.NotificationCompat.Builder;
import java.io.File;

import static android.content.Intent.*;

class NotificationHelper {
    private static final String ACTION_NULL = "com.qihoo.update.action_NULL";
    private static final int NOTIFY_ID = 428344597;
    private static final String TAG = "NotificationHelper";

    NotificationHelper() {
    }

    static void notifyMessage(Context context, String title, String message, PendingIntent intent) {
        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Builder builder = new Builder(context);
        builder.setWhen(System.currentTimeMillis()).setAutoCancel(true).setSmallIcon(context.getApplicationInfo().icon);
        if(!TextUtils.isEmpty(title)) {
            builder.setContentTitle(title);
        }

        if(!TextUtils.isEmpty(message)) {
            builder.setContentText(message);
        }

        builder.setContentIntent(intent);
        nm.cancel(428344597);
        Notification notification = getNotification(builder);
        if(notification != null) {
            nm.notify(428344597, notification);
        } else if(UpdateManager.isDebug()) {
            Log.e("NotificationHelper", "Can not get Notification");
        }

    }

    static void notifyProgressMessage(Context context, String title, String message, PendingIntent intent, int progress, int max, boolean indeterminate) {
        if(VERSION.SDK_INT < 16) {
            notifyProgressMessageApi16(context, title, message, intent, progress, max, indeterminate);
        } else {
            NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            Builder builder = new Builder(context);
            builder.setWhen(System.currentTimeMillis()).setAutoCancel(false).setSmallIcon(context.getApplicationInfo().icon).setOngoing(true).setOnlyAlertOnce(true);
            if(!TextUtils.isEmpty(title)) {
                builder.setContentTitle(title);
            }

            if(!TextUtils.isEmpty(message)) {
                builder.setContentText(message);
            }

            if(!indeterminate && max > 0) {
                builder.setContentInfo(String.format("%.0f%%", new Object[]{Float.valueOf((float)progress / (float)max * 100.0F)}));
            }

            builder.setProgress(max, progress, indeterminate);
            builder.setContentIntent(intent);
            Notification notification = getNotification(builder);
            if(notification != null) {
                nm.notify(428344597, notification);
            } else if(UpdateManager.isDebug()) {
                Log.e("NotificationHelper", "Can not get Notification");
            }
        }

    }

    private static Notification getNotification(Builder builder) {
        return builder == null?null:builder.build();
    }

    static void notifyProgressMessageApi16(Context context, String title, String message, PendingIntent intent, int progress, int max, boolean indeterminate) {
        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Builder builder = new Builder(context);
        builder.setAutoCancel(false).setSmallIcon(context.getApplicationInfo().icon).setOngoing(true).setOnlyAlertOnce(true);
        if(!TextUtils.isEmpty(title)) {
            builder.setContentTitle(title);
        }

        if(!TextUtils.isEmpty(message)) {
            if(!indeterminate && max > 0) {
                message = message + " " + String.format("%.0f%%", new Object[]{Float.valueOf((float)progress / (float)max * 100.0F)});
            }

            builder.setContentText(message);
        }

        builder.setContentIntent(intent);
        Notification notification = getNotification(builder);
        if(notification != null) {
            nm.notify(428344597, notification);
        } else if(UpdateManager.isDebug()) {
            Log.e("NotificationHelper", "Can not get Notification");
        }

    }

    static void notifyDownloadComplete(Context context, DownloadInfo downloadInfo, File file) {
        try {
            Intent e;
            Uri apkUri;
            PendingIntent pendingIntent;
            String title;
            String message;
            if(file.exists()) {
                e = new Intent(context, CheckUpdateService.class);
                e.setAction("com.qihoo.checkupdate.sdk.ACTION_INSTALL_OR_OPEN_APPSTORE");
                apkUri = Uri.fromFile(new File(file.getPath()));
                e.setDataAndType(apkUri, "application/vnd.android.package-archive");
                if(downloadInfo != null) {
                    e.putExtra("DownloadInfo", downloadInfo);
                    e.putExtra("download_file_path", downloadInfo.file);
                }

                pendingIntent = PendingIntent.getService(context, (int)System.currentTimeMillis(), e, FLAG_ACTIVITY_MULTIPLE_TASK);
                title = RHelper.getString(context, "appstore_notification_title_apk_download_ok");
                message = RHelper.getString(context, "appstore_notification_message_apk_download_ok", new Object[]{TextUtils.isEmpty(downloadInfo.title)?"":downloadInfo.title});
                notifyMessage(context, title, message, pendingIntent);
            } else {
                e = new Intent(context, CheckUpdateService.class);
                e.setAction("com.qihoo.checkupdate.sdk.ACTION_INSTALL_OR_OPEN_APPSTORE");
                apkUri = Uri.fromFile(new File(file.getPath()));
                e.setDataAndType(apkUri, "application/vnd.android.package-archive");
                if(downloadInfo != null) {
                    e.putExtra("DownloadInfo", downloadInfo);
                    e.putExtra("download_file_path", downloadInfo.file);
                }

                pendingIntent = PendingIntent.getBroadcast(context, (int)System.currentTimeMillis(), new Intent("ACION_NULL"), FLAG_ACTIVITY_MULTIPLE_TASK);
                title = RHelper.getString(context, "appstore_notification_title_apk_download_fail");
                message = RHelper.getString(context, "appstore_notification_message_apk_download_fail", new Object[]{TextUtils.isEmpty(downloadInfo.title)?"":downloadInfo.title});
                notifyMessage(context, title, message, pendingIntent);
            }
        } catch (Throwable var8) {
            if(UpdateManager.isDebug()) {
                Log.e("NotificationHelper", "notifyDownloadProgress", var8);
            }
        }

    }

    static void notifyDownloadProgress(Context context, String apkTitle, long progress, long max) {
        try {
            String e = RHelper.getString(context, "appstore_notification_title_apk_downloading");
            String message = RHelper.getString(context, "appstore_notification_message_apk_downloading", new Object[]{TextUtils.isEmpty(apkTitle)?"":apkTitle});
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int)System.currentTimeMillis(), new Intent("com.qihoo.update.action_NULL"), FLAG_ACTIVITY_MULTIPLE_TASK);
            if(max > 0L && progress > 0L) {
                notifyProgressMessage(context, e, message, pendingIntent, (int)(100.0F * (float)progress / (float)max), 100, false);
            } else {
                notifyProgressMessage(context, e, message, pendingIntent, 0, 100, true);
            }
        } catch (Throwable var9) {
            if(UpdateManager.isDebug()) {
                Log.e("NotificationHelper", "notifyDownloadProgress", var9);
            }
        }

    }

    public static void notifyNewUpdate(Context context, AppInfo info) {
        if(info.isNewVersion) {
            try {
                PackageManager e = context.getPackageManager();
                CharSequence label = e.getApplicationLabel(e.getApplicationInfo(info.packageName, 0));
                Object label1 = TextUtils.isEmpty(label)?"":label;
                String title = RHelper.getString(context, "appstore_notification_title_found_new_version", new Object[]{label1});
                String message;
                if(info.isPatchUpdate) {
                    message = RHelper.getString(context, "appstore_notification_message_found_new_version_patch", new Object[]{info.versioName, Formatter.formatFileSize(context, info.size - info.patchUpdateSize)});
                } else {
                    message = RHelper.getString(context, "appstore_notification_message_found_new_version_speed", new Object[]{info.versioName, Formatter.formatFileSize(context, info.size)});
                }

                Intent checkUpdateAcitivtyIntent = new Intent(context, CheckUpdateAcitivty.class);
                checkUpdateAcitivtyIntent.addFlags(268435456);
                checkUpdateAcitivtyIntent.putExtra("UpdateInfo", info);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)System.currentTimeMillis(), checkUpdateAcitivtyIntent, FLAG_ACTIVITY_MULTIPLE_TASK);
                notifyMessage(context, title, message, pendingIntent);
            } catch (Throwable var8) {
                if(UpdateManager.isDebug()) {
                    Log.e("NotificationHelper", "notifyDownloadProgress", var8);
                }
            }

        }
    }
}
